﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using COM.Data;
using COM.Web;
using COM.Service;
using SampleApp.Domain;
using SampleApp.Service;
using SampleApp.Web.Service;
using SampleApp.Web.SysModule.Models;

namespace SampleApp.Web.SysModule.Controllers
{
    public class SysUserController : Controller
    {
        private ISysUserService sysUserService = Unity.Instance.GetService<ISysUserService>();
        private ISysRoleService sysRoleService = Unity.Instance.GetService<ISysRoleService>();
        private ISysDeptService sysDeptService = Unity.Instance.GetService<ISysDeptService>();
        private IRuntime runtime = Unity.Instance.GetService<IRuntime>();

        public ActionResult List(int page = 1)
        {
            var list = this.sysUserService.GetSysUserList();
            return View(list);
        }


        public ActionResult Edit(string id)
        {
            return RedirectToAction(EnumOperation.Update, new { id = id });
        }

        public ViewResult Update(string id)
        {
            var sysUser = this.sysUserService.GetSysUserByCode(id);
            return View("Edit", sysUser);
        }

        public ViewResult ReadOnly(string id)
        {
            var sysUser = this.sysUserService.GetSysUserByCode(id);
            return View("Edit", sysUser);
        }

        public ViewResult Role()
        {
            return View();
        }

        public ActionResult Create()
        {
            var sysUser = new SysUser()
            {
                UserCode = Guid.NewGuid().ToString(),
                UserName = "新员工",
            };

            return View("Edit", sysUser);
        }


        [HttpPost]
        public ActionResult Create(SysUser sysUser)
        {
            if (ModelState.IsValid)
            {
                this.sysUserService.CreateSysUser(sysUser);
                return RedirectToAction("List");
            }

            return null;
        }


        [HttpPost]
        public ActionResult Update(SysUser sysUser)
        {
            if (ModelState.IsValid)
            {
                var existsUser = this.sysUserService.GetSysUserByCode(sysUser.UserCode);
                this.runtime.HttpRequestService.Merge(sysUser, existsUser);
                this.sysUserService.UpdatePartialSysUser(existsUser);
                return RedirectToAction("List");
            }
            return null;
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            this.sysUserService.DeleteSysUser(id);
            return null;
        }

        public ActionResult UserRoleList(string id)
        {
            var roleAll = sysRoleService.GetSysRoleList();
            var roleList = this.sysUserService.GetRoleListByUser(id);
            var list = roleList.Select(i => new UserRoleModel() { IsSelected = true, RoleID = i, RoleName = roleAll.Where(r=>r.RoleID == i).Select(r=>r.RoleName).SingleOrDefault() }).ToList();
            //合并未设置的角色
            var q = roleAll.Where(i => !roleList.Any(r => i.RoleID == r)).Select(i => new UserRoleModel() { IsSelected = false, RoleID = i.RoleID, RoleName = i.RoleName }).ToList();
            var model = new UserRoleListModel() { UserCode = id, RoleList = list.Concat(q).ToList() };
            return View(model);
        }

        [HttpPost]
        public ActionResult SetUserRole(UserRoleListModel model)
        {
            var roleList = model.RoleList.Where(i => i.IsSelected).Select(i => i.RoleID).ToList();
            this.sysUserService.SetUserRole(model.UserCode, roleList);
            return null;
        }

        [HttpPost]
        public ActionResult ResetPassword(string id)
        {
            var userCode = id;
            this.sysUserService.SetUserPassword(userCode, userCode);
            return null;
        }

    }
}